package com.jbm.jbmsupplier.zp.adapters;

import android.content.Context;
import android.media.MediaPlayer;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.zp.entity.ChatMsgEntity;
import com.jbm.jbmsupplier.zp.interfaces.SendMeaage;
import com.jbm.jbmsupplier.zp.presenters.ChatBaseActivitys;
import com.jbm.jbmsupplier.zp.views.ChatBaseViews;

import java.util.List;


public class ChatMsgViewAdapter extends BaseAdapter implements SendMeaage, ChatBaseViews {

    private static final String TAG = ChatMsgViewAdapter.class.getSimpleName();
    private List<ChatMsgEntity> coll;
    private String Top;
    private Context ctx;
    private int to_id;
    private JumpInterface listener;
    private ChatBaseActivitys chatBaseActivitys;
    MediaPlayer mMediaPlayer;

    public ChatMsgViewAdapter(Context context, List<ChatMsgEntity> coll, int to_id, int local_id, JumpInterface listener) {
        this.listener = listener;
        ctx = context;
        this.coll = coll;
        this.to_id = to_id;
        chatBaseActivitys = new ChatBaseActivitys(context, this);
        chatBaseActivitys.getToid(to_id);
    }

    public int getCount() {
        return coll.size();
    }

    public Object getItem(int position) {
        return coll.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return coll.get(position).getFlag();


    }

    public int getViewTypeCount() {

        return 30;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            switch (coll.get(position).getFlag()) {
//				case 0:
//					//小秘书是否需要服务
//					convertView = mInflater.inflate(R.layout.secretary_whether, null);
//					break;
//				case 1:
//					//小秘书联系地址选择
//					convertView = mInflater.inflate(R.layout.secretary_contact_info, null);
//					break;
//				case 2:
//					//小秘书维修类别
//					convertView = mInflater.inflate(R.layout.secretary_maintenance_category, null);
//					break;
//				case 3:
//					//小秘书施工队
//					convertView = mInflater.inflate(R.layout.secretary_construction_team, null);
//					break;
//				case 4:
//					//小秘书订单
//					convertView = mInflater.inflate(R.layout.secretary_order, null);
//					break;
//				case 5:
//					//小秘书支付
//					convertView = mInflater.inflate(R.layout.secretary_payment, null);
//					break;
//				case 6:
//					//小秘书评价
//					convertView = mInflater.inflate(R.layout.secretary_evaluate, null);
//					break;
                case 7:
                    //聊天发送的消息
                    convertView = chatBaseActivitys.setLayout(7);
                    break;
                case 8:
                    //聊天接收的消息
                    convertView = chatBaseActivitys.setLayout(8);
                    break;
                case 9:
                    //发送图片
                    convertView = chatBaseActivitys.setLayout(9);
                    break;
                case 10:
                    //接收图片
                    convertView = chatBaseActivitys.setLayout(10);
                    break;
                case 11:
                    //发送语音
                    convertView = chatBaseActivitys.setLayout(11);
                    break;
                case 12:
                    //接收语音
                    convertView = chatBaseActivitys.setLayout(12);
                    break;
//                case 13://发送支付
//                    convertView = chatBaseActivitys.setLayout(13);
//                    break;
//                case 14://接收支付
//                    convertView = chatBaseActivitys.setLayout(14);
//                    break;
            }
            viewHolder = new ViewHolder(convertView, coll.get(position).getFlag());
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //初始化数据
        chatBaseActivitys.initData(viewHolder, position, coll.get(position).getFlag(), coll);
        return convertView;
    }

    public void notifyData(ChatMsgEntity data) {
        coll.add(data);
        listener.scroll();
        notifyDataSetChanged();
    }

    public void notifyData(List<ChatMsgEntity> coll1) {
        coll = coll1;
        notifyDataSetChanged();
    }

    public void notifyData(ChatMsgEntity data, int lastPosition) {
        coll.add(data);
        notifyDataSetChanged();
        if (coll.size() - 2 == lastPosition) {
            listener.scroll();
        }
    }

    @Override
    public void setSucced() {

    }

    @Override
    public void setError() {

    }

    @Override
    public void setUpError(String tag, String name, String date, int cgid, int tags) {//更新失败数据
        listener.setonClickSend(tag, name, date, cgid, tags);
        notifyDataSetChanged();

    }

    @Override
    public void setClckMedia(MediaPlayer mMediaPlayer) {
        this.mMediaPlayer = mMediaPlayer;

    }

    /**
     * 停止语音
     */
    public void killMediaPlayer() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
        }
    }

    public final static class ViewHolder {
        public TextView tv_miaoshu;
        public LinearLayout vodiu_layout;
        public TextView tvSendTime;
        public TextView tvUserName;
        public TextView tvContent;
        public TextView tvTime;
        public TextView btn_secretary_need;
        public TextView btn_secretary_unwanted;
        public TextView tvContactIfo;
        public TextView tvMobile;
        public TextView btn_contant_ok;
        public TextView btn_contact_modify;
        public TextView tv_mainten_categ;
        public TextView btn_mainten_categ_ok;
        public TextView btn_constru_ok;
        public TextView tv_constru_team;
        public TextView tv_order_contact;
        public TextView tv_order_mobile;
        public TextView tv_order_maintenance_category;
        public TextView tv_order_construction_team;
        public TextView btn_order_ok;
        public ListView Lv_payment;
        public TextView btn_payment;
        public TextView btn_evaluate;
        public TextView chatting_open_licensing_address;
        public TextView tv_chatcontent;
        public ImageView av_userhead, imtv_chatcontent, iv_userheads, av_userheadyuyin, image_shibai;
        //支付
        public TextView tv_paydate;
        public TextView tv_pay_money;
        public TextView tv_receivables;
        public TextView tv_receivables_mode;
        public TextView tv_paystate;


        /**
         * 先从小秘书开始   7种 0-6   人7-后
         *
         * @param convertView
         * @param type
         */
        public ViewHolder(View convertView, int type) {
            switch (type) {
//				case 0:
//					tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
//					tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
//					tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
//					btn_secretary_need = (TextView) convertView.findViewById(R.id.btn_secretary_need);
//					btn_secretary_unwanted = (TextView) convertView.findViewById(R.id.btn_secretary_unwanted);
//					break;
//				case 1:
//					tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
//					tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
//					tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
//					tvContactIfo = (TextView) convertView.findViewById(R.id.tv_contact_contact_info);
//					tvMobile = (TextView) convertView.findViewById(R.id.tv_contact_mobile);
//					btn_contant_ok = (TextView) convertView.findViewById(R.id.btn_contant_ok);
//					btn_contact_modify = (TextView) convertView.findViewById(R.id.btn_contact_modify);
//					break;
//				case 2:
//					tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
//					tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
//					tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
//					tv_mainten_categ = (TextView) convertView.findViewById(R.id.tv_mainten_categ);
//					btn_mainten_categ_ok = (TextView) convertView.findViewById(R.id.btn_mainten_categ_ok);
//					break;
//				case 3:
//					tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
//					tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
//					tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
//					tv_constru_team = (TextView) convertView.findViewById(R.id.tv_constru_team);
//					btn_constru_ok = (TextView) convertView.findViewById(R.id.btn_constru_ok);
//					break;
//				case 4:
//					tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
//					tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
//					tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
//					tv_order_contact = (TextView) convertView.findViewById(R.id.tv_order_contact);
//					tv_order_mobile = (TextView) convertView.findViewById(R.id.tv_order_mobile);
//					tv_order_maintenance_category = (TextView) convertView.findViewById(R.id.tv_order_maintenance_category);
//					tv_order_construction_team = (TextView) convertView.findViewById(R.id.tv_order_construction_team);
//					btn_order_ok = (TextView) convertView.findViewById(R.id.btn_order_ok);
//					break;
//				case 5:
//					tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
//					tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
//					tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
//					Lv_payment = (ListView) convertView.findViewById(R.id.Lv_payment);
//					btn_payment = (TextView) convertView.findViewById(R.id.btn_payment);
//					break;
//				case 6:
//					tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
//					tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
//					tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
//					btn_evaluate = (TextView) convertView.findViewById(R.id.btn_evaluate);
//					break;
                case 7:
                    tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
                    tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
                    tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
                    iv_userheads = (ImageView) convertView.findViewById(R.id.iv_userhead);
                    image_shibai = (ImageView) convertView.findViewById(R.id.image_shibai);
                    break;
                case 8:
                    tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
                    tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
                    tvContent = (TextView) convertView.findViewById(R.id.tv_chatcontent);
                    iv_userheads = (ImageView) convertView.findViewById(R.id.iv_userhead);
                    break;
                case 9:
                    tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
                    tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
                    imtv_chatcontent = (ImageView) convertView.findViewById(R.id.iv_chatcontent);
                    iv_userheads = (ImageView) convertView.findViewById(R.id.iv_userhead);
                    image_shibai = (ImageView) convertView.findViewById(R.id.image_shibai);
                    break;
                case 10:
                    tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
                    tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
                    imtv_chatcontent = (ImageView) convertView.findViewById(R.id.iv_chatcontent);
                    iv_userheads = (ImageView) convertView.findViewById(R.id.iv_userhead);
                    break;
                case 11:
                    tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
                    tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
                    av_userhead = (ImageView) convertView.findViewById(R.id.av_chatcontent);
                    iv_userheads = (ImageView) convertView.findViewById(R.id.av_userhead);
                    vodiu_layout = (LinearLayout) convertView.findViewById(R.id.vodiu_layout);
                    tv_miaoshu = (TextView) convertView.findViewById(R.id.tv_miaoshu);
                    image_shibai = (ImageView) convertView.findViewById(R.id.image_shibai);
                    break;
                case 12:
                    tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);
                    tvUserName = (TextView) convertView.findViewById(R.id.tv_username);
                    av_userhead = (ImageView) convertView.findViewById(R.id.av_chatcontent);
                    av_userheadyuyin = (ImageView) convertView.findViewById(R.id.av_userhead);
                    vodiu_layout = (LinearLayout) convertView.findViewById(R.id.vodiu_layout);
                    tv_miaoshu = (TextView) convertView.findViewById(R.id.tv_miaoshu);
                    break;
                case 13:
                    break;
                case 14:
                    tvSendTime = (TextView) convertView.findViewById(R.id.tv_sendtime);//接收时间
                    tv_paydate = (TextView) convertView.findViewById(R.id.tv_paydate);//发送时间
                    tv_pay_money = (TextView) convertView.findViewById(R.id.tv_pay_money);//支付金额
                    tv_receivables = (TextView) convertView.findViewById(R.id.tv_receivables);//收款方
                    tv_receivables_mode = (TextView) convertView.findViewById(R.id.tv_receivables_mode);//支付类型
                    tv_paystate = (TextView) convertView.findViewById(R.id.tv_paystate);//状态
                    break;
            }
        }

    }

    public interface JumpInterface {
        void scroll();
        void setonClickSend(String tag, String name, String date, int cgid, int tags);

    }


}
